#pragma execution_character_set("utf-8")
#include "mainwindow.h"
#include <QApplication>
#include <QTextCodec>
#include <QStyleFactory>
#include <QTime>
#include <QThread>
#include <QDebug>
#include <QDesktopWidget>
#include "frminput2019.h"
#include "project.h"
#include "globaldef.h"
#include "constants.h"
#include "log4z.h"
#include "blldataflow.h"
#include "systempreset.h"
#include "databasemanager.h"
#include <SingleApplication>
#include <QDir>

using namespace zsummer::log4z;

static void init()
{
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
    qApp->setStyle(QStyleFactory::create("Fusion"));
#else
    qApp->setStyle(QStyleFactory::create("Cleanlooks"));
#endif

    //打印下默认字体的名称,方便输入法设置
    QFont font = qApp->font();
    qDebug() << font.family() << font.pointSize() << font.pixelSize();
    qApp->setFont(QFont("Microsoft YaHei", 9));
    //qApp->setFont(QFont("WenQuanYi Micro Hei", 10));

    //设置编码防止乱码
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
#if _MSC_VER
    QTextCodec *codec = QTextCodec::codecForName("gbk");
#else
    QTextCodec *codec = QTextCodec::codecForName("utf-8");
#endif
    QTextCodec::setCodecForLocale(codec);
    QTextCodec::setCodecForCStrings(codec);
    QTextCodec::setCodecForTr(codec);
    qApp->setInputContext(0);
#else
    QTextCodec *codec = QTextCodec::codecForName("utf-8");
    QTextCodec::setCodecForLocale(codec);
#endif
}
static void readParam()
{
    gLanguage = SystemPreset::instance()->getStringData(EnumPresetItem::Language);
    gSpeedRatio = SystemPreset::instance()->getDoubleData(EnumPresetItem::FlowRateCalibCoef);
    gVoltRatio = SystemPreset::instance()->getDoubleData(EnumPresetItem::VoltageCalibCoef);
    gTableWidth = SystemPreset::instance()->getIntData(EnumPresetItem::TableWidth);
}
static void initMultiLang()
{
    readParam();
    translator = new QTranslator();
    bool state = translator->load(TranslationFile);
    qDebug() << "translator load bool:" << state;
    LOGFMTD("translator load bool: %d", state);
    qApp->installTranslator(translator);
}
static void initLogSystem()
{
    ILog4zManager::getRef().setLoggerPath(LOG4Z_MAIN_LOGGER_ID, LogDir.toLatin1().data());
    ILog4zManager::getRef().setLoggerLevel(LOG4Z_MAIN_LOGGER_ID, LOG_LEVEL_TRACE);
    ILog4zManager::getRef().setLoggerOutFile(LOG4Z_MAIN_LOGGER_ID, true);
    ILog4zManager::getRef().start();
    QString currentPath = QDir::currentPath();
    QString applicationDirPath = qApp->applicationDirPath();
    QString applicationFilePath = qApp->applicationFilePath();
    LOGFMTD("currentPath: %s",currentPath.toStdString().c_str());
    LOGFMTD("applicationDirPath: %s",applicationDirPath.toLatin1().data());
    LOGFMTD("applicationFilePath: %s",applicationFilePath.toLatin1().data());
}

static bool initDatabase()
{
    bool ret = true;
    DatabaseManager databaseManager;
    ret = ret && databaseManager.create(DbPath);
    ret = ret && databaseManager.upgrade();
    Q_ASSERT(ret);
    return ret;
}

int main(int argc, char *argv[])
{
    SingleApplication app(argc, argv);

    // 如果程序已运行，唤醒到前台
    QObject::connect(&app, &SingleApplication::instanceStarted, []() {
        Project::Instance()->bringToFront();
    });

    initLogSystem();
    initDatabase();

    QTime startTime = QTime::currentTime();
    init();
    QTime endTime = QTime::currentTime();
    int elapsed = startTime.msecsTo(endTime);
    qDebug() << "init style font TextCodec time: " << elapsed;
    LOGFMTD("init style font TextCodec time: %d ms",elapsed);

    startTime = QTime::currentTime();
    initMultiLang();
    endTime = QTime::currentTime();
    elapsed = startTime.msecsTo(endTime);
    qDebug() << "initMultiLang time: " << elapsed;
    LOGFMTD("initMultiLang time: %d ms",elapsed);

     startTime = QTime::currentTime();
    //virtual keyboard initialization
    frmInput2019::Instance()->hide();
    frmInput2019::Instance()->setFontInfo("MicroSoft Yahei", 18, 16);
    frmInput2019::Instance()->setFixedSize2(650, 260);
    frmInput2019::Instance()->setIconSize(26, 26);
    frmInput2019::Instance()->setTopHeight(0);
    endTime = QTime::currentTime();
    elapsed = startTime.msecsTo(endTime);
    qDebug() << "virtual keyboard init time: " << elapsed;
    LOGFMTD("virtual keyboard init time: %d ms",elapsed);

    startTime = QTime::currentTime();
    windowWidth = QApplication::desktop()->width();

    // Project换成单例模式
    Project::Instance()->show();

    // project构建完成后发送构建完成的信号，在弹窗页面将窗口设置setWindowFlags(windowFlags() | Qt::SubWindow);同时设置该窗口的父窗口为Project，即能解决多窗口键盘穿透bug
    BllDataFlow::getInstance()->buildFinished();
    endTime = QTime::currentTime();
    elapsed = startTime.msecsTo(endTime);
    qDebug() << "Project time: " << elapsed;
    LOGFMTD("Project init time: %d ms",elapsed);

    return app.exec();
}
